<?php
	require_once 'ctlAbstract.php';
	require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busLocation.php';
	require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busProvider.php';

	class LocationController extends AbstractController {
	/**
	 * This template variable will hold the 'view' portion of our MVC for this
	 * controller
	 */
	private $_locationBus;
	private $_providerBus;
	
	/**
	 *
	 * This is the default function that will be called by the router
	 * @param array $getVars GET variables posted to index.php
	 */
	public function main(array $getVars) {		
		$this->_locationBus = new LocationBus();
		$this->_providerBus = new ProviderBus();
		$this->_resourceBus = new ResourceBus();
		
		if ($getVars['action'] == 'get') {			
			$parent = $_POST['parentID'];
			$locationList = $this->_locationBus->getLocationList($parent);
			$xml = '<?xml version="1.0" encoding="UTF-8" ?>';
			$xml .= '<locations>';
			
			foreach ($locationList as $location) {
				$xml .= '<location id="' . $location->getLocationID() . '" name="' . $location->getName() . '" />';				 
			}
			$xml .= '</locations>';
			echo $xml;
		} else if ($getVars['action'] == 'load') {
			if (isset($_POST['region'])) {
				$location = $this->_locationBus->getLocationByRegion($_POST['region']);
				echo $location->getLocationID();
			}
		} else if ($getVars['action'] == 'loadByJSON') {
			if (isset($_POST['region'])) {
				$location = $this->_locationBus->getLocationById($_POST['region']);
				$providers = $this->_providerBus->findProviderDTOsByLocationID($location->getLocationID());
				$locationByJSON = $this->renderObjByArray($location, $providers);
				
				echo json_encode($locationByJSON);
			}
		} else if ($getVars['action'] == 'loadAllByParentByJSON') {
			$parent = $_POST['parentID'];
			$locationList = $this->_locationBus->getLocationList($parent);
			$lst = array();
			foreach ($locationList as $location) {
				$arr = $this->renderLocationByArray($location);
				$lst[] = $arr;
			}
			echo json_encode($lst);
		}		
	}

	private function renderLocationByArray($location) {
		global $ubi;
		$jsonLocationArr = array('id' => $location->getLocationID(), 'name' => $location->getName(), 'type' => $location->getType(), 
			'latitude' => $location->getLatitude(), 'longitude' => $location->getLongitude(), 
			'zoom' => $ubi['SITE']['ZOOM'][$location->getType()]);
		return $jsonLocationArr;
	}
	
	private function renderObjByArray($location, $providers) {
		global $ubi;
		$jsonProviderArrs = array();
		foreach ($providers as $providerDTO) {
			$jsonProviderArr = array('id' => $providerDTO->getID(), 'name' => $providerDTO->getName(), 
				'lat' => $providerDTO->getAddress()->getLatitude(), 'long' => $providerDTO->getAddress()->getLongitude(),
				'img' => $ubi['SERVER']['THUMBNAIL_PATH'] . '/' . $providerDTO->getImg()->getResourceName(),
				'address' => $providerDTO->getAddress()->getNumber() . ' ' . $providerDTO->getAddress()->getStreet() . ' '
					. $providerDTO->getAddress()->getWard() . ' ' . $providerDTO->getAddress()->getDistrict(),
				'link' => 'index.php?view&type=provider&id=' . $providerDTO->getId());
			$jsonProviderArrs[] = $jsonProviderArr; 
		}
		$jsonLocationArr = array('id' => $location->getLocationID(), 'name' => $location->getName(), 'type' => $location->getType(), 
			'latitude' => $location->getLatitude(), 'longitude' => $location->getLongitude(), 
			'zoom' => $ubi['SITE']['ZOOM'][$location->getType()]);
		$arr = array('location' => $jsonLocationArr, 'providers' => $jsonProviderArrs);
		
		return $arr;
	}
}
?>